/**
 * 
 */
package com.xbang.attendance.service.impl;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.xbang.attendance.entity.role.User;
import com.xbang.attendance.service.ExcelExportService;
import com.xbang.core.dao.search.component.SearchComponent;
import com.xbang.core.service.impl.BaseServiceImpl;

/**
 * @author dev.zhang
 * 
 */
public class ExcelExportServiceImpl extends BaseServiceImpl implements ExcelExportService {

	public File exportExcel(String hqlSentence, SearchComponent searchComponent, List<String> exportFields) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public File exportExcel(String hqlSentence, Map<String, Object> parameters, List<String> exportFields) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public File exportExcel(String hqlSentence, SearchComponent searchComponent) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public File exportExcel(String hqlSentence, Map<String, Object> parameters) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 把list集合中数据保存到xls文件中
	 * 
	 * @return
	 */
	public InputStream getExcelFile()// getExcelFile()一定要与excelFile对应，否则会出现异常
	{
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("sheet1");

		HSSFRow row = sheet.createRow(0);// 创建第一行

		HSSFCell cell = row.createCell(0); // 创建第一列
		cell.setCellValue(new HSSFRichTextString("id"));

		cell = row.createCell(1);// 创建第二列
		cell.setCellValue(new HSSFRichTextString("姓名"));

		cell = row.createCell(2);// 创建第三列
		cell.setCellValue(new HSSFRichTextString("年龄"));

//		List<User> list = new ExcelDownloadAction().getList();
		List<User> list = new ArrayList<User>();
		for (int i = 0; i < list.size(); i++) {
			User user = (User) list.get(i);

			row = sheet.createRow(i + 1);// 创建第i+1行

			cell = row.createCell(0);// 创建第一列
			cell.setCellValue(user.getId());

			cell = row.createCell(1);// 创建第二列
			cell.setCellValue(new HSSFRichTextString(user.getName()));

			cell = row.createCell(2);// 创建第三列
			cell.setCellValue(122);

		}

		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		try {
			workbook.write(baos);// 写入
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		byte[] ba = baos.toByteArray();
		ByteArrayInputStream bais = new ByteArrayInputStream(ba);
		return bais;
	}

}
